package gw.com.android.presenter;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.gwtsz.gts2.hx.R;

import www.com.library.util.DeviceUtil;

/**
 * recycler view 添加一个返回到顶部的通用控件
 *
 * @author jett
 * @since 2018-08-20.
 */
public class BackTopPresenter {

    private final Activity activity;
    private final RecyclerView rvList;

    private int mHeight = -1;
    private int distance = 0;

    private PopupWindow pw;
    private boolean calcNavigationBar = false;
    private int mGravity;
    private int mX;
    private int mY;

    public BackTopPresenter(Activity activity, RecyclerView rvList) {
        this.activity = activity;
        this.rvList = rvList;
        mGravity = Gravity.BOTTOM | Gravity.RIGHT;
        mX = DeviceUtil.instance().dip2px(13, activity);
        mY = DeviceUtil.instance().dip2px(50, activity);
        init();
    }

    private void init() {
        RecyclerView.LayoutManager manager = rvList.getLayoutManager();
        if (manager instanceof LinearLayoutManager) {
            rvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                }

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    distance += dy;
                    onScroll(distance);
                }
            });
        }
    }

    public BackTopPresenter location(int gravity, int x, int y) {
        this.mGravity = gravity;
        this.mX = x;
        this.mY = y;
        return this;
    }

    /**
     * popwindow 显示时，如果recyclerview被导航栏挡住，popwindow的下边距需重新计算
     */
    public BackTopPresenter calcNavigationBar(boolean calcNavigationBar) {
        this.calcNavigationBar = calcNavigationBar;
        return this;
    }

    private void onScroll(int distance) {
        if (mHeight <= 0) {
            mHeight = rvList.getMeasuredHeight();
        }
        if (mHeight <= 0) {
            return;
        }
        if (distance > mHeight) {
            showTopIcon();
        } else {
            hideTopIcon();
        }
    }

    private void showTopIcon() {
        if (pw == null) {
            ImageView ivTop = new ImageView(rvList.getContext());
            ivTop.setImageResource(R.mipmap.a_backtop);
            ivTop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    scrollTop();
                }
            });
            pw = new PopupWindow(ivTop, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            pw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            pw.setFocusable(false);
            pw.setOutsideTouchable(false);
            pw.setAnimationStyle(R.style.popp_anim);

            if (calcNavigationBar) {
                mY += DeviceUtil.instance().getNavigationBarHeight(activity);
            }
        }
        if (!pw.isShowing()) {
            pw.showAtLocation(rvList, mGravity, mX, mY);
        }
    }

    private void scrollTop() {
        rvList.smoothScrollToPosition(0);
    }

    private void hideTopIcon() {
        if (pw != null && pw.isShowing()) {
            pw.dismiss();
        }
    }

}
